﻿--领取充值补偿福利

RechargeAwardConfig =
{
	limitLevel = 45,
	time = {2012, 11, 26, 18, 0, 0,  2012, 11, 28, 18, 0, 0},
	AwardMap = 
	{
		--成就由低到高 按顺序 100块钱
		{
			achieveId = 137, 
			awards = 
			{
				{ type = 0, id = 1036, count = 1, bind = 1, },
			},
		},
		--200块钱
		{
			achieveId = 138, 
			awards = 
			{
				{ type = 0, id = 549, count = 1, bind = 1, job = 1, },
				{ type = 0, id = 550, count = 1, bind = 1, job = 2, },
				{ type = 0, id = 551, count = 1, bind = 1, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
			},
		},
		--500块钱
		{
			achieveId = 139, 
			awards = 
			{
				{ type = 0, id = 549, count = 1, bind = 1, job = 1, },
				{ type = 0, id = 550, count = 1, bind = 1, job = 2, },
				{ type = 0, id = 551, count = 1, bind = 1, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
				{ type = 0, id = 1031, count = 3, bind = 1, },
				{ type = 0, id = 971, count = 1, bind = 1, },
			},
		},
		--1000块钱
		{
			achieveId = 140, 
			awards = 
			{
				{ type = 0, id = 549, count = 1, bind = 1, job = 1, },
				{ type = 0, id = 550, count = 1, bind = 1, job = 2, },
				{ type = 0, id = 551, count = 1, bind = 1, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
				{ type = 0, id = 1031, count = 3, bind = 1, },
				{ type = 0, id = 1119, count = 1, bind = 1, },
				{ type = 0, id = 971, count = 2, bind = 1, },
			},
		},
		--2000块钱
		{
			achieveId = 141, 
			awards = 
			{
				{ type = 0, id = 549, count = 1, bind = 1, job = 1, },
				{ type = 0, id = 550, count = 1, bind = 1, job = 2, },
				{ type = 0, id = 551, count = 1, bind = 1, job = 3, },
				
				{ type = 0, id = 453, count = 1, bind = 0, job = 1, },
				{ type = 0, id = 454, count = 1, bind = 0, job = 2, },
				{ type = 0, id = 455, count = 1, bind = 0, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
				{ type = 0, id = 1031, count = 3, bind = 1, },
				{ type = 0, id = 1121, count = 1, bind = 1, },
				{ type = 0, id = 971, count = 2, bind = 1, },
			},
		},
		--5000块钱
		{
			achieveId = 142, 
			awards = 
			{
				{ type = 0, id = 549, count = 1, bind = 1, job = 1, },
				{ type = 0, id = 550, count = 1, bind = 1, job = 2, },
				{ type = 0, id = 551, count = 1, bind = 1, job = 3, },
				
				{ type = 0, id = 453, count = 1, bind = 0, job = 1, },
				{ type = 0, id = 454, count = 1, bind = 0, job = 2, },
				{ type = 0, id = 455, count = 1, bind = 0, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
				{ type = 0, id = 1031, count = 3, bind = 1, },
				{ type = 0, id = 1121, count = 2, bind = 1, },
				{ type = 0, id = 971, count = 2, bind = 1, },
			},
		},
		--1w块钱
		{
			achieveId = 143, 
			awards = 
			{
				{ type = 0, id = 1092, count = 1, bind = 0, job = 1, },
				{ type = 0, id = 1096, count = 1, bind = 0, job = 2, },
				{ type = 0, id = 1100, count = 1, bind = 0, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
				{ type = 0, id = 1031, count = 3, bind = 1, },
				{ type = 0, id = 1121, count = 2, bind = 1, },
				{ type = 0, id = 1108, count = 1, bind = 1, },
			},
		},
		--3w块钱
		{
			achieveId = 144, 
			awards = 
			{
				{ type = 0, id = 1092, count = 1, bind = 0, job = 1, },
				{ type = 0, id = 1096, count = 1, bind = 0, job = 2, },
				{ type = 0, id = 1100, count = 1, bind = 0, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
				{ type = 0, id = 924, count = 1, bind = 1, },
				{ type = 0, id = 804, count = 1, bind = 1, },
				{ type = 0, id = 1031, count = 3, bind = 1, },
				{ type = 0, id = 1123, count = 2, bind = 1, },
				{ type = 0, id = 1108, count = 1, bind = 1, },
			},
		},
		--5w块钱
		{
			achieveId = 145, 
			awards = 
			{
				{ type = 0, id = 1092, count = 1, bind = 0, job = 1, },
				{ type = 0, id = 1096, count = 1, bind = 0, job = 2, },
				{ type = 0, id = 1100, count = 1, bind = 0, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
				{ type = 0, id = 784, count = 1, bind = 1, },
				{ type = 0, id = 794, count = 1, bind = 1, },
				{ type = 0, id = 1031, count = 3, bind = 1, },
				{ type = 0, id = 1123, count = 2, bind = 1, },
				{ type = 0, id = 1108, count = 1, bind = 1, },
			},
		},
		--10w块钱
		{
			achieveId = 146, 
			awards = 
			{
				--{ type = 0, id = 1125, count = 1, bind = 0, },
				{ type = 0, id = 1092, count = 1, bind = 0, job = 1, },
				{ type = 0, id = 1096, count = 1, bind = 0, job = 2, },
				{ type = 0, id = 1100, count = 1, bind = 0, job = 3, },
				{ type = 0, id = 1036, count = 1, bind = 1, },
				{ type = 0, id = 784, count = 1, bind = 1, },
				{ type = 0, id = 794, count = 1, bind = 1, },
				{ type = 0, id = 1031, count = 3, bind = 1, },
				{ type = 0, id = 1123, count = 2, bind = 1, },
				{ type = 0, id = 1108, count = 1, bind = 1, },
			},
		},

	},
}

--[[
    函数名称：
    函数描述：NPC交互入口
    参数 sysarg 的描述：
]]
function RechargeAward_Main(sysarg)

	local config = RechargeAwardConfig
	if (not config) then return "" end
	local str = ""
	if System.isInDateRange(unpack(config.time)) == true then
		str = str.."\n<#BL".."领取充值补偿福利".."/@GetRechargeAward>"  --<领取充值补偿福利>
	end
	return str
end

function GetRechargeAward(sysarg)
	local config = RechargeAwardConfig
	if (not config) then return "" end
	local str = ""
	if System.isInDateRange(unpack(config.time)) == true then
		str = "补偿时间为2012-11-26到2012-11-28的18点正。\n在这3天之内，根据您已经领取的充值礼包，可以领取同一档次的充值福利包。无论您领取哪个档次的礼包，您只能领取一次充值补偿福利。"
		str = str.."\n<#BL".."确定领取补偿福利".."/@OnGetRechargeAward>"  --<确定领取补偿福利>
	end
	return str
end

function OnGetRechargeAward(sysarg)
	local config = RechargeAwardConfig
	if Actor.checkActorLevel(sysarg, config.limitLevel, 0) ~= true then
		Actor.sendTipmsg(sysarg, string.format("等级不足%d，无法领取",config.limitLevel), ttFlyTip)
		return
	end
	
	local svar = Actor.getStaticVar(sysarg)
	if svar.RechargeAward ~= nil then 
		Actor.sendTipmsg(sysarg, "您已领取过奖励无法再次领取", ttFlyTip)
		return 
	end
	
	
	
	if System.isInDateRange(unpack(config.time)) ~= true then
		Actor.sendTipmsg(sysarg, "领奖时间已过", ttFlyTip)
		return
	end
	
	
	local index = 0
	local count = table.getn(config.AwardMap)
	for i = count, 1, -1 do
		if Actor.isAchieveFinished(sysarg, config.AwardMap[i].achieveId) == true then
			index = i
			break
		end
	end
	
	
	if index == 0 then
		Actor.sendTipmsg(sysarg, "您没有达成任何充值礼包成就，无法领取此福利", ttFlyTip)
		return
	end
	local map = config.AwardMap[index]
	if map == nil then return end
	
	local awards = map.awards
	if Awards.CheckBagGridCount(sysarg, awards) ~= true then
		return
	end
	svar.RechargeAward = 1
	
	Awards.Give(sysarg, awards, 101, "领取充值补偿福利")
	Actor.sendTipmsg(sysarg, "恭喜您领取奖励成功", ttFlyTip)
	
	
	return
end


Awards =
{
	--检测背包空间 awards:奖励表
	CheckBagGridCount = function (sysarg, awards)
		if not awards then return false, 1000000 end
		local needCount = 0
		local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
		for k,v in pairs(awards) do
			if v.type == 0 then
				local quality = v.quality or 0
				local strong = v.strong or 0
				local bind = v.bind or 0
				local param = v.param or nil
				local guid = 0
				local job = v.job or 0
				if job == 0 or job == vocation then
					if v.type == 0 and type(param) ~= 'number' and param ~= nil then
						v.id = Item.getItemProperty(sysarg, param, Item.ipItemID, 0)  --获得物品的ID 
					end
					local count = math.floor(v.count)
					needCount = needCount + Item.getAddItemNeedGridCount(sysarg, v.id, count, quality, strong, bind, guid)
				end
			end
		end
		local hasCount = Item.getBagEmptyGridCount(sysarg)
		if hasCount < needCount then 
			Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.mt00001, needCount), ttFlyTip)
			return false, needCount
		end
		return true, needCount
	end,


	--给予奖励 awards:奖励表 logId:日志ID logStr:日志表述
	
	Give = function (sysarg, awards, logId, logStr)
		if not awards then return false end	
		local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
		for k,v in pairs(awards) do
			local quality = v.quality or 0
			local strong = v.strong or 0
			local bind = v.bind or 1
			local param = v.param or nil
			local guid = v.param  or 0
			local job = v.job or 0
			if job == 0 or job == vocation then
				if v.type == 0 and type(param) ~= 'number' and param ~= nil then
					v.id = Item.getItemProperty(sysarg, param, Item.ipItemID, 0)  --获得物品的ID 
				end
				local count = math.floor(v.count)
				if Actor.giveAward(sysarg, v.type, v.id, count, quality, strong, bind, guid, logId, logStr) ~= true then
					System.trace(string.format("Error:Give award error! give type = %d, id = %d, count = %d", v.type, v.id, count))
					return false
				end
			end
		end
		return true
	end,
}


table.insert(MainFnTable, RechargeAward_Main)